import 'package:flutter/material.dart';

class HistoryOverlay extends StatefulWidget {
  final String title;
  final VoidCallback onClose;
  final Widget child;

  const HistoryOverlay({
    super.key,
    required this.title,
    required this.onClose,
    required this.child,
  });

  @override
  State<HistoryOverlay> createState() => HistoryOverlayState();
}

class HistoryOverlayState extends State<HistoryOverlay> {
  late Offset position;
  late Offset dragStartPosition;
  late Offset widgetStartPosition;

  @override
  void initState() {
    super.initState();
    // 初始化默认位置
    position = const Offset(0, 0);

    // 初始位置在屏幕中央偏上
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (mounted) {
        final RenderBox renderBox = context.findRenderObject() as RenderBox;
        final Size size = renderBox.size;
        final Size screenSize = MediaQuery.of(context).size;
        setState(() {
          position = Offset(
            screenSize.width - size.width,
            screenSize.height * 0.2,
          );
        });
      }
    });
  }

  void _handleDragStart(DragStartDetails details) {
    dragStartPosition = details.globalPosition;
    widgetStartPosition = position;
  }

  void _handleDragUpdate(DragUpdateDetails details) {
    setState(() {
      // 计算新位置
      Offset newPosition =
          widgetStartPosition + (details.globalPosition - dragStartPosition);

      // 获取屏幕尺寸
      final Size screenSize = MediaQuery.of(context).size;

      // 获取当前widget的尺寸
      final RenderBox renderBox = context.findRenderObject() as RenderBox;
      final Size widgetSize = renderBox.size;

      // 限制X轴位置，确保窗口不移出左右边界
      if (newPosition.dx < 0) {
        newPosition = Offset(0, newPosition.dy);
      } else if (newPosition.dx + widgetSize.width > screenSize.width) {
        newPosition = Offset(
          screenSize.width - widgetSize.width,
          newPosition.dy,
        );
      }

      // 限制Y轴位置，确保窗口不移出上下边界
      if (newPosition.dy < 0) {
        newPosition = Offset(newPosition.dx, 0);
      } else if (newPosition.dy + widgetSize.height > screenSize.height) {
        newPosition = Offset(
          newPosition.dx,
          screenSize.height - widgetSize.height,
        );
      }

      position = newPosition;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      left: position.dx,
      top: position.dy,
      child: GestureDetector(
        onPanStart: _handleDragStart,
        onPanUpdate: _handleDragUpdate,
        child: Container(
          width: 150,
          constraints: const BoxConstraints(maxHeight: 400),
          decoration: BoxDecoration(
            color: Theme.of(context).cardColor,
            borderRadius: BorderRadius.circular(12),
            boxShadow: [
              BoxShadow(color: Colors.black38, blurRadius: 15, spreadRadius: 1),
            ],
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 标题栏
              Container(
                padding: const EdgeInsets.fromLTRB(10, 0, 0, 0),
                decoration: BoxDecoration(
                  color: Theme.of(context).primaryColor.withValues(alpha: 0.1),
                  borderRadius: const BorderRadius.vertical(
                    top: Radius.circular(12),
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      widget.title,
                      style: TextStyle(
                        fontSize: 18,
                        color:
                            Theme.of(context).textTheme.bodyMedium?.color ??
                            Colors.black,
                        decoration: TextDecoration.none,
                      ),
                    ),
                    // 关闭按钮
                    IconButton(
                      icon: const Icon(Icons.close),
                      onPressed: widget.onClose,
                    ),
                  ],
                ),
              ),
              // 内容区域
              SizedBox(height: 350, child: widget.child),
            ],
          ),
        ),
      ),
    );
  }
}
